package com.foreknow.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * 过滤字符编码集
 */
public class CharacterEncoding implements Filter {
    private String encoding = "ISO8859-1";
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        //客户端向服务器端发送请求的字符编码集为utf-8
        request.setCharacterEncoding(encoding);
        //服务器端向客户端返回的内容类型
        response.setContentType("text/html;charset=utf-8");
        //交给下一个过滤器来处理，如果没有其它过滤器了，就将请求交给Servlet来处理
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
